import jsonref
import yaml
import json

# 加载 YAML 文件
with open("test.yaml", 'r') as f:
    spec = yaml.safe_load(f)

# 使用 jsonref 解析
resolved_spec = jsonref.replace_refs(spec)

# 标记递归节点
def detect_and_mark_recursion(schema, visited=None):
    if visited is None:
        visited = set()

    if isinstance(schema, dict):
        if "$ref" in schema:
            ref = schema["$ref"]
            if ref in visited:
                return {"_recursive_ref": ref}  # 标记递归引用
            visited.add(ref)

        for key, value in schema.items():
            schema[key] = detect_and_mark_recursion(value, visited)
    elif isinstance(schema, list):
        for i in range(len(schema)):
            schema[i] = detect_and_mark_recursion(schema[i], visited)

    return schema

# 打印解析后的规范
print(json.dumps(resolved_spec, indent=2))
